package net.sxnic.jhitem.subject.dao;

import java.util.List;

import net.sxnic.jhitem.subject.Subject;
import net.sxnic.jhitem.subject.dao.SubjectDao;
import net.sxinfo.core.spring25.HibernateDao;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

@Repository("subjectDao")
public class HibernateSubjectDao extends HibernateDao<Subject, String> implements
	SubjectDao {

	/**
	 * 获取所有一级学科
	 * @return 一级学科列表
	 */
	public List<Subject> getTopLevels()
	{
		//一级学科编码长度为3
		String hql = "from Subject where length(subjectCode)=3 order by subjectCode";
		return this.find(hql);
	}
	
	
	/**
	 * 根据一级学科代码查询所属二级学科
	 * @param parent 一级学科编码
	 * @return 一级学科对应的二级学科列表
	 */
	public List<Subject> getSubLevel2(String parent)
	{
		if (StringUtils.isBlank(parent)) {
			return null;
		}
		if (parent.length() % 3 != 0) {
			return null;
		}
		int len = parent.length();
		String hql = "from Subject where length(subjectCode)="
				+ String.valueOf(len + 3) + " and subjectCode like '" + parent
				+ "%' order by subjectCode";
		return this.find(hql);
	}
	
	
	/**
	 * 根据二级学科代码查询所属三级学科
	 * @param parent 二级学科编码
	 * @return 二级学科对应的三级学科列表
	 */
	public List<Subject> getSubLevel3(String parent)
	{
		if (StringUtils.isBlank(parent)) {
			return null;
		}
		if (parent.length() % 3 != 0) {
			return null;
		}
		int len = parent.length();
		
		String hql = "from Subject where length(subjectCode)="
				+ String.valueOf(len + 2) + " and subjectCode like '" + parent
				+ "%' order by subjectCode";
		return this.find(hql);
	}
	
	/**
	 * 根据学科编码获取学科名称
	 * @param code 学科编码
	 * @return 学科编码对应的学科名称
	 */
	public String getValueByCode(String code)
	{
		if (StringUtils.isBlank(code)) {
			return "";
		}
		String hql = "from Subject where subjectCode=?";
		//根据学科编码获取学科实体
		Subject subject = this.findUnique(hql, new Object[]{code});
		if(subject!=null)
		{
			return subject.getSubjectName();
		}
		return "";
	}
}
